#include <string>

#include <opencv2/opencv.hpp>

using namespace std;

void ROIExtraction(unsigned char *Image, unsigned char *SubImage, int Sx[4], int Sy[4]);

void assign_roi_coordinates(cv::Mat &img, int Sx[4], int Sy[4], float resize_factor)
{
    cv::Point result[1][4];

    auto set_coordinates_1 = [](cv::Point result[1][4]) -> void {
        //0102_014
        result[0][0] = cv::Point2d(205, 161);
        result[0][1] = cv::Point2d(397, 126);
        result[0][2] = cv::Point2d(432, 319);
        result[0][3] = cv::Point2d(239, 354);
    };
    auto set_coordinates_2 = [](cv::Point result[1][4]) -> void {
        //IMG_0000_010_LLL_
        result[0][0] = cv::Point2d(474, 237);
        result[0][1] = cv::Point2d(551, 434);
        result[0][2] = cv::Point2d(354, 512);
        result[0][3] = cv::Point2d(276, 315);
    };

    set_coordinates_2(result);

    Sx[0] = result[0][3].y * resize_factor;
    Sx[1] = result[0][2].y * resize_factor;
    Sx[2] = result[0][1].y * resize_factor;
    Sx[3] = result[0][0].y * resize_factor;

    Sy[0] = result[0][3].x * resize_factor;
    Sy[1] = result[0][2].x * resize_factor;
    Sy[2] = result[0][1].x * resize_factor;
    Sy[3] = result[0][0].x * resize_factor;
}

int main(int argc, char **argv)
{
    const int roi_size = 128;
    const float resize_factor = 0.4f;
    const string filepath = "/home/yjy/dataset/palmprint_dectection_tongji/LHand/palmvein_val/IMG_0000_010_LLL_.jpg";
    const string savepath = "";

    int Sx[4], Sy[4]; //ROI坐标
    //uchar *result_data = new uchar[roi_size * roi_size * sizeof(uchar)];
    cv::Mat img = cv::imread(filepath);
    cv::Mat result = cv::Mat::zeros(cv::Size(roi_size, roi_size), CV_8U);

    assign_roi_coordinates(img, Sx, Sy, resize_factor);
    cv::resize(img, img, cv::Size(), resize_factor, resize_factor);
    cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
    //debug
    for (int i = 0; i < 4; i++)
        cv::circle(img, cv::Point(Sy[i], Sx[i]), 2 * i + 1, cv::Scalar(0));

    ROIExtraction(img.data, result.data, Sx, Sy);

    cv::imshow("img", img);
    cv::imshow("result", result);
    cv::waitKey(0);
    //cv::imwrite(savepath, result);
}